class Pointer:ScriptObject
{
	void update()
	{
		set_world_position(entity,(get_bird_world_pos()+float3(-2,15,5)));
/*		
		float4x4 projmtx=getprojectionmtx(deg2rad(60),30,160);
		float4x4 ip; ip.invert(projmtx);
		
		float4x4 mtx=get_bird_matrix();
		float3 p=mtx.translate; 
		float3 t; 
		t=p;
		t.z=(1+t.z)*1.4; 
		t.y/=get_aspect();
		t.z=clamp(t.z,-1,1);
		float mul=1.2;
		t.x=clamp(t.x,-mul,mul);
		float screen_aspect=get_aspect()*mul;
		t.y=clamp(t.y,-mul,mul);
		float4 p4(t.x,t.y,t.z,1);
		p4=ip.mul(p4);
		
		p4.w=abs(p4.w);
		mtx.translate=float3(p4.x/p4.w,p4.y/p4.w,p4.z/p4.w);
		set_local_matrix(entity,mtx);
*/		
	}
}


const float kopter_maxspeed=2f;
const float kopter_scale=0.8f;
const float kopter_colli_radius=5.0f;
bool demo = false;
class Kopter:ScriptObject
{
	float3 speed;
	float timer;
	float act_scale;
	
	void init()
	{
		speed=float3(0,0,0);
		playsfx("kopter_loop",true);
		timer=0;
	}
	
	void update()
	{
		timer+=1/60.0f;
		int pointer=get_entity("target");
		if (demo)
		{
		pointer=get_entity("demo_target");
		}
		
		act_scale=kopter_scale;
		
		float3 tgt=get_world_position(pointer);
		float3 src=get_world_position(entity);
		deactivate(get_entity("touchthecopter"));
		
		/*if(start_game)
		{
			act_scale=(sin(timer*8))*0.15f+kopter_scale;
			float4x4 boxmtx=get_world_matrix(entity);
			boxmtx.normalize();
			boxmtx.scale(act_scale);
			set_world_matrix(entity,boxmtx);
		
//			print_screen(800,100,"Touch the Kopter to start!");
			if((tgt-src).length()<3)
			{
			
				start_game = false;
			}
		}
		if(!start_game)
		{*/
			float3 tgtspeed=(tgt-src)*0.05f;
			float tl=tgtspeed.length();
			if (tl>kopter_maxspeed)
			{
				tgtspeed*=kopter_maxspeed/tl;
			}
			float3 s=speed;
			float3 accel=(tgtspeed-speed)*0.1f;
			speed+=accel;
			speed*=0.965f;
			accel=speed;
			accel.y=0;
			float3 rotaxis; rotaxis=cross(accel,float3(0,1,0));
			rotaxis.normalize();
			float4x4 mtxsrc=get_world_matrix(entity);
			float y,p,r;
			float4x4 mtxdst=mtxsrc;
			mtxdst.to_rotation(y,p,r);
			mtxdst.from_rotation(y,0,0);
			mtxdst.rotate(rotaxis,accel.length()*-0.2);
			mtxsrc.axisY=mtxsrc.axisY+0.7f*(mtxdst.axisY-mtxsrc.axisY);
			float3 dir=tgtspeed;
			dir.normalize();
			mtxsrc.axisZ=mtxsrc.axisZ+0.01f*(dir-mtxsrc.axisZ);
			
			mtxsrc.normalize_yzx();
			mtxsrc.scale(act_scale);
			set_world_matrix(entity,mtxsrc);
			float3 dst=src+speed;
			
			float3 hitpos,hitnormal;
			if (sphere_background_collision(dst, kopter_colli_radius*kopter_scale,hitpos, hitnormal))
			{
				float d=dot(speed,hitnormal);
				if (d<0)
				{
					speed-=d*hitnormal;
					speed*=0.5f;
					dst=hitpos+speed;
				}
			}
			set_world_position(entity,dst);
		//}
	}
}


Entity cam_path;
float scene_center_dist=150;

class LeoCamera:ScriptObject
{
	float3 scene_center;
	
	void init()
	{
		cam_path = get_entity("cam_01");
//		scene_center=get_world_position(entity)+scene_center_dist*get_world_matrix(entity).axisZ;
		scene_center=get_world_position(get_entity("level_01"));//+scene_center_dist*get_world_matrix(entity).axisZ;
	}
	void update()
	{
		if (key_down(KEY_NUMPAD_MINUS))
			scene_center_dist-=0.2f;
		else if (key_down(KEY_NUMPAD_PLUS))
			scene_center_dist+=0.2f;
		
		int pointer=get_entity("rotocopter");
		float3 tgt=get_world_position(pointer);
		float3 src=get_world_position(entity);
		
		if(!start_game || true)
		{
			
		
		float3 mp=mouse_pos();
		
		
		
		float4x4 mtx=get_world_matrix(entity);
		/*if (mouse_button_down(0))
		{
			float yaw,pitch,roll;
			mtx.to_rotation(yaw,pitch,roll);
			yaw+=0.006f*mp.x;
			pitch-=0.006f*mp.y;
			pitch=clamp(pitch,deg2rad(-89),deg2rad(89));
			mtx.from_rotation(yaw,pitch,roll);
		}
		
		if (bird_small_button_down())
		{
			float yaw,pitch,roll;
			mtx.to_rotation(yaw,pitch,roll);
			float3 birdpos=get_bird_pos();
			
			float yawspeed=0;
			if (birdpos.x<0)
			{
				yawspeed=-0.02f*(birdpos.x);
			}
			else if (birdpos.x>0)
			{
				yawspeed=-0.02f*(birdpos.x);
			}
			
			yaw+=yawspeed;
			
			float pitchspeed=0;
			if (birdpos.y<0)
			{
				pitchspeed=birdpos.y;
			}
			else if (birdpos.y>0)
			{
				pitchspeed=birdpos.y;
			}
			
//			pitch+=pitchspeed;
			mtx.from_rotation(yaw,pitch,roll);
			scene_center.y+=pitchspeed;
		}
		*/
	
		mtx.translate=scene_center-scene_center_dist*mtx.axisZ;
//		mtx.translate=mtx.translate+(mouse_button_down(1) ? 0.5f : 0)*mtx.axisZ;
//		mtx.translate=mtx.translate-(mouse_button_down(2) ? 0.5f : 0)*mtx.axisZ;
		float3 c_tgt=get_world_position(cam_path);
		src +=((c_tgt - src)*0.001f);
		
		set_world_matrix(entity,mtx);
//		set_world_position(entity,src);
		}
	}
}
